package cn.edu.cug.cs.gtl.apps.ui;

import cn.edu.cug.cs.gtl.apis.render.Render;
import cn.edu.cug.cs.gtl.apis.ui.*;
import cn.edu.cug.cs.gtl.apis.ui.catalogtree.CatalogPanel;
import cn.edu.cug.cs.gtl.apis.ui.devicetree.DevicePanel;
import cn.edu.cug.cs.gtl.apis.ui.projecttree.ProjectPanel;
import cn.edu.cug.cs.gtl.apps.ui.actions.AppExitActionListener;

import javax.swing.*;
import java.awt.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class MainWindow extends AbstractFrame {


    JSplitPane  splitPane = null;
    JTabbedPane tabbedPane = null;

    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu modellingMenu = new JMenu("Modelling");
    JMenu analysisMenu = new JMenu("Analysis");
    JMenu viewMenu = new JMenu("View");
    JMenu toolMenu = new JMenu("Tool");
    JMenuItem pluginMenuItem = new JMenuItem("Plugin...");
    JMenu helpMenu = new JMenu("Help");

    JToolBar fileToolBar = new JToolBar("File");
    JToolBar editToolBar = new JToolBar("Edit");
    JToolBar modellingToolBar = new JToolBar("Modelling");
    JToolBar analysisToolBar = new JToolBar("Analysis");
    JToolBar viewToolBar = new JToolBar("View");
    JToolBar toolToolBar = new JToolBar("Tool");
    JToolBar helpToolBar = new JToolBar("Help");


    public MainWindow(MainApp theApp) throws HeadlessException {
        this.theApp = theApp;
    }

    public MainWindow(MainApp   theApp,GraphicsConfiguration gc) {
        super(gc);
        this.theApp = theApp;
    }

    public MainWindow(MainApp   theApp,String title) throws HeadlessException {
        super(title);
        this.theApp = theApp;
    }

    public MainWindow(MainApp   theApp,String title, GraphicsConfiguration gc) {
        super(title, gc);
        this.theApp = theApp;
    }



    @Override
    public Render getCurrentRender() {
        return this.renderPanel.getRender();
    }

    @Override
    public JMenu getFileMenu() {
        return fileMenu;
    }

    @Override
    public JMenu getEditMenu() {
        return editMenu;
    }

    @Override
    public JMenu getViewMenu() {
        return viewMenu;
    }

    @Override
    public JMenu getToolMenu() {
        return toolMenu;
    }

    @Override
    public JMenu getHelpMenu() {
        return helpMenu;
    }

    @Override
    public JToolBar getFileToolBar() {
        return fileToolBar;
    }

    @Override
    public JToolBar getViewToolBar() {
        return viewToolBar;
    }

    @Override
    public JToolBar getEditToolBar() {
        return editToolBar;
    }

    @Override
    public JToolBar getToolToolBar() {
        return toolToolBar;
    }

    @Override
    public JToolBar getHelpToolBar() {
        return helpToolBar;
    }

    @Override
    public JMenu getModellingMenu() {
        return modellingMenu;
    }

    @Override
    public JMenu getAnalysisMenu() {
        return analysisMenu;
    }

    @Override
    public JToolBar getModellingToolBar() {
        return modellingToolBar;
    }

    @Override
    public JToolBar getAnalysisToolBar() {
        return analysisToolBar;
    }

    public void initial(){
        initialMenus();
        initialRenderPanel();
        initialProjectPanel();
        initialTablePanel();
        initialDevicePanel();
        initialCatalogPanel();
        initialTabbedPane();

        getContentPane().setLayout(new BorderLayout());
        JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT,this.renderPanel, this.tablePanel);
        right.setDividerLocation(800);
        right.setEnabled(true);
        splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tabbedPane,right);
        splitPane.setEnabled(true);
        splitPane.setDividerLocation(300);

        getContentPane().add(splitPane,BorderLayout.CENTER);
        initialTooBars();

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    private void initialTabbedPane(){
        tabbedPane = new JTabbedPane();
        ResourceBundle resourceBundle=ResourceBundle.getBundle("app_resource", Locale.getDefault());
        tabbedPane.addTab(resourceBundle.getString("data.model3d"),this.projectPanel);
        tabbedPane.addTab(resourceBundle.getString("data.catalog"),this.catalogPanel);
        tabbedPane.add(resourceBundle.getString("data.device"),this.devicePanel);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
        tabbedPane.setSelectedIndex(0);
    }

    private void initialDevicePanel(){
        devicePanel = new DevicePanel(theApp);
        devicePanel.initial();
    }

    private void initialCatalogPanel(){
        catalogPanel = new CatalogPanel(theApp);
        catalogPanel.initial();
    }

    private void initialRenderPanel(){
        renderPanel = new RenderPanel(theApp);
        String s = null;//"D:" + File.separator + "temp" + File.separator + "test.h3d";
        getRenderPanel().initial(s);
    }

    private void initialTablePanel(){
        tablePanel = new TablePanel(theApp);
        tablePanel.initial();
    }
    private void initialProjectPanel(){
        projectPanel = new ProjectPanel(theApp);
        projectPanel.initial();
    }
    private void initialMenus()
    {
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);


        ResourceBundle resourceBundle=ResourceBundle.getBundle("app_resource", Locale.getDefault());

        JMenuBar menubar = new JMenuBar();

        fileMenu.setText(resourceBundle.getString("menu.file"));
        menubar.add(fileMenu);

        JMenuItem appExitItem = new JMenuItem(resourceBundle.getString("menu.file.exit"));
        appExitItem.addActionListener(new AppExitActionListener(theApp));
        fileMenu.add(appExitItem);

        editMenu.setText(resourceBundle.getString("menu.edit"));
        menubar.add(editMenu);

        modellingMenu.setText(resourceBundle.getString("menu.modelling"));
        menubar.add(modellingMenu);

        analysisMenu.setText(resourceBundle.getString("menu.analysis"));
        menubar.add(analysisMenu);

        viewMenu.setText(resourceBundle.getString("menu.view"));
        menubar.add(viewMenu);

        toolMenu.setText(resourceBundle.getString("menu.tool"));
        menubar.add(toolMenu);

        pluginMenuItem.setText(resourceBundle.getString("menu.tool.plugin"));
        toolMenu.add(pluginMenuItem);

        helpMenu.setText(resourceBundle.getString("menu.help"));
        menubar.add(helpMenu);

        this.setJMenuBar(menubar);
    }
    private void initialTooBars(){
        JPanel toobarPanel = new JPanel(new FlowLayout(0));
        toobarPanel.add(fileToolBar,FlowLayout.LEFT);
        toobarPanel.add(editToolBar,FlowLayout.LEFT);
        toobarPanel.add(modellingToolBar,FlowLayout.LEFT);
        toobarPanel.add(analysisToolBar,FlowLayout.LEFT);
        toobarPanel.add(viewToolBar,FlowLayout.LEFT);
        toobarPanel.add(toolToolBar,FlowLayout.LEFT);
        toobarPanel.add(helpToolBar,FlowLayout.LEFT);

        getContentPane().add(toobarPanel,BorderLayout.NORTH);
    }


}
